<?php
namespace app\wechat\controller;

class MemberRoleInfo extends Common
{
    // 设置页面
    public function save_role_info() {
        $id = session('member_id');
        $param['member_id'] = $id;
        $param['type'] = input('param.type');

        $data = call_interface('get_member_role_info', $param);
        $member_info = $data['data'];
        // 如果头像和昵称不存在则使用微信头像和昵称
        isset($member_info['nickname']) && $member_info['nickname'] ? $member_info['nickname'] : $member_info['nickname'] = session('nickname');
        isset($member_info['headimgurl']) && $member_info['headimgurl'] ? $member_info['headimgurl'] : $member_info['headimgurl'] = session('headimgurl');
        $this->assign('member_info', $member_info);
        $this->assign('type', $param['type']);
        switch ($param['type']) {
            case 1:
                $url = url('r_member/index');
                break;
            case 2:
                $url = url('d_member/index');
                break;
            case 3:
                $url = url('p_member/index');
                break;
            case 4:
                $url = url('s_member/index');
                break;
            default:
                $url = url('member/index');
                break;
        }
        $this->assign('url', $url);
        return view();
    }

    // 设置用户信息
    public function save_member_role_info() {
        $member_id = session('member_id');
        if (request()->isPost()) {
            $param = [
                'member_id' => $member_id,
                'type' => input('param.type'),
                'field' => input('param.field'),
                'value' => input('param.value')
            ];
            if ($param['field'] == 'birthday') {
                $param['value'] = strtotime($param['value']);
            }
            //dump($param);
            $data = call_interface('save_role_info', $param);
            if ($data['code'] == 200) {
                $data['url'] = url('save_role_info', ['type' => input('param.type')]);
            }
            return json($data);
        } else {
            $param = [
                'member_id' => $member_id,
                'type' => input('param.type'),
                'field' => input('param.field'),
                'value' => input('param.value')
            ];
            $data = call_interface('get_field_info', $param);
            $this->assign('type', $param['type']);
            $this->assign('value', $data['data']['value']);
            $this->assign('field', $param['field']);
            return view();
        }
    }

    // 实名认证
    public function certification() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $member_id = session('member_id');

        $param = [
            'member_id' => $member_id,
            'type' => 0,
        ];
        $data = call_interface('certification_info', $param);
        $this->assign('data', $data['data']);
        return view();
    }

    // 修改实名认证
    public function save_certification() {
        $type = input('param.type');
        if (request()->isAjax()) {
            $param = [
                'member_id' => session('member_id'),
                'type' => $type,
                'proposer' => input('param.proposer'),
                'card_no' => input('param.card_no'),
                'id_card_in_hand' => input('param.id_card_in_hand'),
                'id_card_front' => input('param.id_card_front'),
                'id_card_verso' => input('param.id_card_verso')
            ];
            $data = call_interface('save_certification', $param);
            switch ($type) {
                case 1:
                    $url = url('r_member/index');
                    break;
                case 2:
                    $url = url('d_member/index');
                    break;
                case 3:
                    $url = url('p_member/index');
                    break;
                case 4:
                    $url = url('s_member/index');
                    break;
                default:
                    $url = url('member/index');
                    break;
            }
            $this->assign('url', $url);
            $data['url'] = $url;
            return json($data);
        } else {

            $member_id = session('member_id');
            $param = [
                'member_id' => $member_id,
                'type' => $type,
            ];
            $data = call_interface('certification_info', $param);
            $this->assign('data', $data['data']['certification_info']);
            $this->assign('type', $type);
            return view();
        }
    }
}
